package com.woniu.controller;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniu.utils.BaseController;
import com.woniu.utils.MyPage;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.woniu.entity.User;
import com.woniu.service.UserService;

@Controller
@RequestMapping("/auction/User")
public class UserController extends BaseController {
	@Autowired
	private UserService userService;


	@Autowired  
	private DefaultKaptcha captchaProducer;
	
	
	//分页查询
	@RequestMapping("/userSelectByCond")
	@ResponseBody
	public MyPage<?> userSelectByCond(String uName, String uEmail, String uPhone, String uIdcard, int pageNum, int pageSize) {
		return userService.userSelectByCond(uName, uEmail, uPhone, uIdcard, pageNum, pageSize);
	}
	
	
	//设置用户状态
	@RequestMapping("/updateAccountStatus")
	@ResponseBody
	public String updateAccountStatus(int uId, String value) {
		User user = userService.selectUserById(uId);
		if("禁止登录".equals(value))
			user.setAccountstatus("正常");
		else
			user.setAccountstatus("禁止登录");
		System.out.println(user.getAccountstatus());
		
		String str = "";
		if(userService.updateUser(user)==1){
			str = userService.selectUserById(uId).getAccountstatus();
		}else{
			str = "false";
		}
		
		return str;
	}
	
	
	
	//删除用户
	@RequestMapping("/deleteUserById")
	@ResponseBody
	public String deleteUserById(int uId) {
		int res = userService.deleteUserById(uId);
		return res==1?"true":"false";
	}
	 
	//查找用户，用于用户名形式唯一性验证
	@RequestMapping("/selectUserByName")
	@ResponseBody
	public String selectUserByName(String uName) {
		return userService.selectUserByName(uName);
	}
	
	
	//新建用户
	@RequestMapping("/createUser")
	@ResponseBody
	public String createUser(String uName, String uLoginPass, String uBuyPass, String uEmail, String uPhone, 
				             String uIdCard, String userStatus, String accountStatus) {
		
		User user = new User();
		user.setUname(uName);
		user.setUpass(uLoginPass);
		user.setAccountpassword(uBuyPass);
		user.setEmail(uEmail);
		user.setTel(uPhone);
		user.setIdcard(uIdCard);
		user.setUserstatus(userStatus);
		user.setAccountstatus(accountStatus);
		user.setUtime(new Date());
		
		return userService.createUser(user)==1?"true":"false";
	}
	
	
	//修改用户之查找用户，跳转页面
	@RequestMapping("/goUpdateUser")
	public String goUpdateUser(int uId,Model model) {
		User user = userService.selectUserById(uId);
		model.addAttribute("adminuser", user);
		return "paipai/adminiframe/updateUser";
	}
	
	//修改用户
	@RequestMapping("/updateUser")
	@ResponseBody
	public String updateAdmin(int uId ,String uName, String uLoginPass, String uBuyPass, String uEmail, String uPhone, 
            String uIdCard, String userStatus, String accountStatus) {
		System.out.println(uId);
		User user = new User();
		user.setUid(uId);
		user.setUname(uName);
		user.setUpass(uLoginPass);
		user.setAccountpassword(uBuyPass);
		user.setEmail(uEmail);
		user.setTel(uPhone);
		user.setIdcard(uIdCard);
		user.setUserstatus(userStatus);
		user.setAccountstatus(accountStatus);
		user.setUtime(userService.selectUserById(uId).getUtime());

		return userService.updateUser(user)==1?"true":"false";
	}
	
	
	@RequestMapping("/defaultKaptcha")
	public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
			throws Exception {
		byte[] captchaChallengeAsJpeg = null;
		ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
		try {
			// 生产验证码字符串并保存到session中
			String createText = captchaProducer.createText();
			httpServletRequest.getSession().setAttribute("vrifyCode", createText);
			// 使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中
			BufferedImage challenge = captchaProducer.createImage(createText);
			ImageIO.write(challenge, "jpg", jpegOutputStream);
		} catch (IllegalArgumentException e) {
			httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
			return;
		}

		// 定义response输出类型为image/jpeg类型，使用response输出流输出图片的byte数组
		captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
		httpServletResponse.setHeader("Cache-Control", "no-store");
		httpServletResponse.setHeader("Pragma", "no-cache");
		httpServletResponse.setDateHeader("Expires", 0);
		httpServletResponse.setContentType("image/jpeg");
		ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream();
		responseOutputStream.write(captchaChallengeAsJpeg);
		responseOutputStream.flush();
		responseOutputStream.close();
	}


}